/**
    Copyright (C) 2011  Alexander Vollmer

    This program is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */
package org.jefb.entity;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;

import javax.persistence.CascadeType;
import javax.persistence.CollectionTable;
import javax.persistence.Column;
import javax.persistence.ElementCollection;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToMany;
import javax.persistence.Table;

import org.jefb.sec.entity.UserRight;
import org.springframework.security.core.GrantedAuthority;

@Entity
@Table(name = "CV_ROLE")
public class RoleEntity extends BasicEntity implements GrantedAuthority, IBasicEntity {
	private static final long serialVersionUID = -7016813452354468665L;

	private String authority;
	private String description;

	private Collection<UserEntity> users;
	
	private List<UserRight> rights;

	@Column(name = "AUTHORITY")
	public String getAuthority() {
		return authority;
	}

	@Column(name = "DESCRIPTION", nullable = true)
	public String getDescription() {
		return description;
	}

	@ElementCollection(fetch = FetchType.LAZY, targetClass = UserRight.class)
	@CollectionTable(name = "CV_ROLE_RIGHT", joinColumns = @JoinColumn(name = "ROLE_ID"))
	public List<UserRight> getRights() {
		if(rights==null){
			rights = new ArrayList<UserRight>();
		}
		return rights;
	}
	
	@ManyToMany(fetch=FetchType.LAZY, cascade=CascadeType.ALL, mappedBy="userRoles")
	public Collection<UserEntity> getUsers() {
		return users;
	}

	public void setAuthority(String authority) {
		this.authority = authority;
	}

	public void setRights(List<UserRight> rights) {
		this.rights = rights;
	}

	public void setDescription(String description) {
		this.description = description;
	}

	public void setUsers(Collection<UserEntity> users) {
		this.users = users;
	}

	@Override
	public int hashCode() {
		final int prime = 31;
		int result = 1;
		result = prime * result
				+ ((authority == null) ? 0 : authority.hashCode());
		result = prime * result + ((id == null) ? 0 : id.hashCode());
		return result;
	}

	@Override
	public boolean equals(Object obj) {
		if (this == obj)
			return true;
		if (obj == null)
			return false;
		if (getClass() != obj.getClass())
			return false;
		RoleEntity other = (RoleEntity) obj;
		if (authority == null) {
			if (other.authority != null)
				return false;
		} else if (!authority.equals(other.authority))
			return false;
		if (id == null) {
			if (other.id != null)
				return false;
		} else if (!id.equals(other.id))
			return false;
		return true;
	}

}
